package com.pic.mgt.unit.operation;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import com.pic.mgt.log.IgxLog;
import com.pic.mgt.unit.Message;
import com.pic.mgt.unit.Unit;
import com.pic.mgt.utils.LogUtils;

public class SimpleMessageUnitOperation extends SimpleUnitOperation implements
		MessageUnitOperation {

	@Override
	public void copyTransactionFile(Message message, File destFolder)
			throws UnitOperationException {
		try {
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog
						.debug(
								this,
								"Message '{}' operation: copying transaction file '{}' to folder '{}'.",
								message.getUnitName(), message.getTransactionFilename(), destFolder
										.getAbsolutePath());
			}
			// ********************** Log **********************
			
			super.simpleFileOperation.copyFileToFolder(
					message.getTransactionFile(), destFolder);
			
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog
						.debug(
								this,
								"Message '{}' operation: copying transaction file '{}' to folder '{}' completed.",
								message.getUnitName(), message.getTransactionFilename(), destFolder
										.getAbsolutePath());
			}
			// ********************** Log **********************
		} catch (IOException e) {
			try {
				throw new UnitOperationException(
						"Message '" + message.getUnitName() + "' operation: copying transaction file '" +
						message.getTransactionFilename() + "' to folder '" +
						destFolder.getAbsolutePath() + "' failed.", e);
			} catch (UnitOperationException e1) {
				// ********************** Log **********************
				LogUtils.error(this, IgxLog.messageDetailsLog, e1);
				// ********************** Log **********************
				
				throw e1;
			}
		}
	}
	
	@Override
	public void moveTransactionFile(Message message, File destFolder)
			throws UnitOperationException {
		try {
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog
						.debug(
								this,
								"Message '{}' operation: moving transaction file '{}' to folder '{}'.",
								message.getUnitName(), message.getTransactionFilename(), destFolder
										.getAbsolutePath());
			}
			// ********************** Log **********************
			
			File newTransactionFile =
					super.simpleFileOperation.moveFileToFolder(message
							.getTransactionFile(), destFolder);
			message.setTransactionFile(newTransactionFile);
			
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog
						.debug(
								this,
								"Message '{}' operation: moving transaction file '{}' to folder '{}' completed.",
								message.getUnitName(), message.getTransactionFilename(), destFolder
										.getAbsolutePath());
			}
			// ********************** Log **********************
		} catch (IOException e) {
			try {
				throw new UnitOperationException(
						"Message '" + message.getUnitName() + "' operation: moving transaction file '" +
						message.getTransactionFilename() + "' to folder '" +
						destFolder.getAbsolutePath() + "' failed.", e);
			} catch (UnitOperationException e1) {
				// ********************** Log **********************
				LogUtils.error(this, IgxLog.messageDetailsLog, e1);
				// ********************** Log **********************
				
				throw e1;
			}
		}
	}
	
	@Override
	public void removeConfirmationFileForTxFile(Message message)
			throws UnitOperationException {
		try {
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog
						.debug(
								this,
								"Message '{}' operation: removing confirmation file '{}' for transaction file.",
								message.getUnitName(), message.getConfirmationFileForTxFile().getName());
			}
			// ********************** Log **********************
			
			super.simpleFileOperation.deleteFile(message.getConfirmationFileForTxFile());
			
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog
						.debug(
								this,
								"Message '{}' operation: removing confirmation file '{}' for transaction file completed.",
								message.getUnitName(), message.getConfirmationFileForTxFile().getName());
			}
			// ********************** Log **********************
		} catch (IOException e) {
			try {
				throw new UnitOperationException(
						"Message '" + message.getUnitName() +
						"' operation: removing confirmation file '" +
						message.getConfirmationFileForTxFile().getName() +
						"' for transaction file failed.",
						e);
			} catch (UnitOperationException e1) {
				// ********************** Log **********************
				LogUtils.error(this, IgxLog.messageDetailsLog, e1);
				// ********************** Log **********************
				
				throw e1;
			}
		}
	}

	@Override
	public void removeTransactionFile(Message message) throws UnitOperationException {
		try {
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog
						.debug(
								this,
								"Message '{}' operation: removing transaction file '{}'.",
								message.getUnitName(), message.getTransactionFilename());
			}
			// ********************** Log **********************
			
			super.simpleFileOperation.deleteFile(message.getTransactionFile());
			
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog
						.debug(
								this,
								"Message '{}' operation: removing transaction file '{}' completed.",
								message.getUnitName(), message.getTransactionFilename());
			}
			// ********************** Log **********************
		} catch (IOException e) {
			try {
				throw new UnitOperationException(
						"Message '" + message.getUnitName() +
						"' operation: removing transaction file '" +
						message.getTransactionFilename() +"' failed.",
						e);
			} catch (UnitOperationException e1) {
				// ********************** Log **********************
				LogUtils.error(this, IgxLog.messageDetailsLog, e1);
				// ********************** Log **********************
				
				throw e1;
			}
		} 
	}

	@Override
	public void removeUnit(Unit unit) throws UnitOperationException {
		Message message = (Message)unit;
		
		// 刪除交易檔的確認檔
		try {
			this.removeConfirmationFileForTxFile(message);
		} catch (UnitOperationException e) {
			Throwable causeThrowable  = e.getCause();
			if (FileNotFoundException.class.isInstance(causeThrowable)) {
				// 如果是屬於找不到檔案的錯誤，則忽略之。
			}
			else {
				throw e;
			}
		}
		
		// 刪除交易檔
		try {
			this.removeTransactionFile(message);
		} catch (UnitOperationException e) {
			Throwable causeThrowable  = e.getCause();
			if (FileNotFoundException.class.isInstance(causeThrowable)) {
				// 如果是屬於找不到檔案的錯誤，則忽略之。
			}
			else {
				throw e;
			}
		}
		
		// 刪除訊息確認檔
		try {
			super.removeConfirmationFile(unit);
		} catch (UnitOperationException e) {
			Throwable causeThrowable  = e.getCause();
			if (FileNotFoundException.class.isInstance(causeThrowable)) {
				// 如果是屬於找不到檔案的錯誤，則忽略之。
			}
			else {
				throw e;
			}
		}
	}

}
